package com.masterclient.app.businessmodule.minemodule.footprint

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.eoner.baselib.utils.toast.ToastUtil
import com.eoner.baselib.widget.navgation.FCNavigationBar
import com.masterclient.app.R
import com.eoner.baselibrary.bean.goods.ProductBean
import com.masterclient.app.businessmodule.minemodule.footprint.adapter.FootPrintAdapter
import com.eoner.baselibrary.bean.personal.FootprintBean
import com.eoner.baselibrary.utils.ScreenUtils
import com.eoner.common.config.BusinessConfig
import com.eoner.middlelib.router.map.FCRouterPath
import com.masterclient.app.base.ConfirmDialog
import com.masterclient.app.base.FCBusinessActivity
import com.masterclient.app.businessmodule.minemodule.footprint.contract.FootprintContract
import com.masterclient.app.businessmodule.minemodule.footprint.contract.FootprintPresenter
import kotlinx.android.synthetic.main.activity_footprint.*
import java.util.*
import kotlin.collections.ArrayList

/**
 * 作者：sean
 * 创建时间：2018/5/21 17:07
 * 功能说明：
 */
@Route(path = FCRouterPath.MINE_GOODS_HISTORY_PATH)
class FootprintActivity() :FCBusinessActivity<FootprintPresenter>(),FootprintContract.View{

    var page = 1
    var treeMap = TreeMap<String, ArrayList<ProductBean>>(Comparator<String> { o1, o2 ->
        when {
            o1>o2 -> -1
            o1<o2 -> 1
            else -> 0
        }
    })

    lateinit var footPrintAdapter:FootPrintAdapter

    override fun useRefreshLayout(): Boolean {
        return true
    }

    override fun requireGetLayoutId(): Int {
       return R.layout.activity_footprint
    }

    override fun requireSetCustomNavigationBar(navigationBar: FCNavigationBar?) {
        navigationBar?.setTitle("我的足迹")
                ?.setLineHidden(false)
                ?.setRightBtnText("清除")
                ?.setDrawableLeftImage(R.mipmap.icon_footer_clear,ScreenUtils.dp2px(3.5F))
    }

    override fun requireInitUIAndData(savedInstanceState: Bundle?) {
        footPrintAdapter = FootPrintAdapter()
        recyclerView.layoutManager = LinearLayoutManager(mContext);
        recyclerView.adapter = footPrintAdapter
        footPrintAdapter.bindToRecyclerView(recyclerView)
        footPrintAdapter.emptyView = View.inflate(mContext,R.layout.empty_msg,null)
        footPrintAdapter.emptyView.visibility = View.GONE
        val  titleView = footPrintAdapter.emptyView.findViewById(R.id.tv_title) as TextView
        titleView.text = "您还没有足迹哦"
        val  tv_msg = footPrintAdapter.emptyView.findViewById(R.id.tv_msg) as TextView
        tv_msg.text = "浏览商品留下您的足迹吧~"
        footPrintAdapter.setFooterView(View.inflate(mContext,R.layout.view_foot4,null))
        footPrintAdapter.footerLayout.visibility = View.GONE
        mPresenter.getProductHistory(page.toString(),BusinessConfig.PAGE_SIZE,false)

        baseRefreshLayout().setOnRefreshListener {

            page = 1
            mPresenter.getProductHistory(page.toString(), BusinessConfig.PAGE_SIZE,true)
        }
        footPrintAdapter.setOnLoadMoreListener(BaseQuickAdapter.RequestLoadMoreListener {
            if(mPresenter != null){
                mPresenter.getProductHistory(page.toString(), BusinessConfig.PAGE_SIZE,false)
            }
        },recyclerView)
    }

    override fun onNavigationRightClick() {
        super.onNavigationRightClick()
        val confirmDialog = ConfirmDialog(mContext, "请谨慎删除“我的足迹”，删除后无法恢复，确认删除么？", "清除", "取消")
        confirmDialog.setOnChooseListerner(object : ConfirmDialog.OnChooseListerner {
            override fun cancel() {
                confirmDialog.dismiss()
                mPresenter.clearFootPrintInfo()
            }

            override fun confirm() {
                confirmDialog.dismiss()
            }
        })
        confirmDialog.show()
    }

    override fun onClickPageStateErrorView() {
        super.onClickPageStateErrorView()
        mPresenter.getProductHistory(page.toString(),BusinessConfig.PAGE_SIZE,false)
    }

    override fun showHistoryData(dataBean: FootprintBean.DataBean) {
        if(dataBean.sh_items!=null){
            if (page == 1) {
                treeMap.clear();
            }
            if(dataBean.sh_items.size>0){
                for(dataItem in dataBean.sh_items){
                    if(treeMap.containsKey(dataItem.sh_date)){
                        val arr =treeMap[dataItem.sh_date]
                        arr!!.add(dataItem)
                    }else{
                        val arrs = ArrayList<ProductBean>()
                        arrs.add(dataItem)
                        treeMap[dataItem.sh_date] = arrs
                    }
                }
                val list = treeMap.toList()
                footPrintAdapter.setNewData(list)
                if (dataBean.sh_items.size > 0) {
                    if (list.size <= 20) {
                        footPrintAdapter.setNewData(list)
                    } else {
                        footPrintAdapter.notifyItemRangeInserted(list.size - dataBean.sh_items.size, dataBean.sh_items.size)
                        footPrintAdapter.loadMoreComplete()
                    }
                }
                footPrintAdapter.setEnableLoadMore(true)
                footPrintAdapter.emptyView.visibility = View.GONE
                page ++;
            } else {
                if(treeMap.isEmpty()){
                    if(page == 1){
                        footPrintAdapter.setNewData(treeMap.toList())
                        footPrintAdapter.emptyView.visibility = View.VISIBLE
                    }else{
                        footPrintAdapter.emptyView.visibility = View.VISIBLE
                    }
                }
                footPrintAdapter.setEnableLoadMore(false)
            }

            if (dataBean.sh_items == null || dataBean.sh_items.size == 0) {
                footPrintAdapter.footerLayout.setVisibility(View.VISIBLE)
            } else {
                //            adapter.setEnableLoadMore(true);
                footPrintAdapter.footerLayout.setVisibility(View.GONE)
            }
            footPrintAdapter.notifyDataSetChanged()
        } else{
            if(treeMap.isEmpty()){
                footPrintAdapter.emptyView.visibility = View.VISIBLE
            }
            footPrintAdapter.setEnableLoadMore(false)
        }
        baseEndRefresh()
    }

    override fun baseShowError() {
        super.baseShowError()
        baseEndRefresh()
        footPrintAdapter.emptyView.visibility = View.GONE
        if (page > 1) {
            footPrintAdapter.loadMoreFail()
        } else {
            footPrintAdapter.setEnableLoadMore(false)
        }
    }

    override fun clearFailed(msg: String?) {
        ToastUtil.showToast(msg)
    }

    override fun clearSuccess() {
        treeMap.clear()
        page = 1
        mPresenter.getProductHistory(page.toString(), BusinessConfig.PAGE_SIZE,false)
    }

}